package com.manor.paradise.activity

import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.lxj.xpopup.XPopup
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.manor.common.Constants
import com.manor.common.activity.AbsActivity
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.ClickUtil
import com.manor.common.utils.GsonUtil
import com.manor.common.utils.KLog
import com.manor.common.utils.ToastUtil
import com.manor.paradise.R
import com.manor.paradise.adapter.AddressAdapter
import com.manor.paradise.bean.AddressBean
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import kotlinx.android.synthetic.main.activity_shipping.*

/**
 * 收货地址
 */

class ShippingAddressActivity : AbsActivity() {
    internal var pageNum = 1
    internal lateinit var mAdapter: AddressAdapter
    override fun getLayoutId(): Int {
        return R.layout.activity_shipping
    }
    companion object {
        fun forward(context: Activity, item: String) {
            val intent = Intent(context, ShippingAddressActivity::class.java)
            intent.putExtra(Constants.ED_SHIPPING, item)
            context.startActivityForResult(intent , 101)
        }
    }
    override fun main() {
        super.main()
        im_back.setOnClickListener({ finish() })
        mAdapter = AddressAdapter(R.layout.item_address, null)
        rcv_list.adapter = mAdapter
        refreshLayout.finishRefresh()
        mAdapter.addChildClickViewIds(R.id.ll_mr, R.id.tv_delete)
        mAdapter.setOnItemChildClickListener(itemChildClickListener)
        mAdapter.setOnItemClickListener { adapter, view, position ->
            if (ClickUtil.canClick()) {
                if (null != intent.extras && null != intent.extras.getString(Constants.ED_SHIPPING)) {
                    val intent = Intent()
                    intent.putExtra(Constants.GIFT_CITY, mAdapter.getItem(position)) //将计算的值回传回去
                    setResult(102, intent)
                    finish() //结束当前的activity的生命周期
                }
            }
        }
        rcv_list.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageNum++
                setData()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageNum = 1
                setData()
            }
        })

        tv_add.setOnClickListener {
            val intent = Intent(this, AddAddressActivity::class.java)
            startActivity(intent)
        }
    }

    override fun onResume() {
        super.onResume()
        setData()
    }
    fun setData() {
        ElvesHttpUtil.getAddrList(pageNum, object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    return
                }
                var addressBean: AddressBean = GsonUtil.gsonToBean<AddressBean>(info, AddressBean::class.java)
                KLog.e("json", addressBean);
                if (pageNum == 1) {
                    if(addressBean.records.size==0){
                        tv_null.visibility = View.VISIBLE
                    }else{
                        tv_null.visibility = View.GONE
                    }

                    mAdapter.setNewInstance(addressBean.records)
                    refreshLayout.finishRefresh()
                } else {
                    mAdapter.addData(addressBean.records)
                    refreshLayout.finishLoadMore()
                }

            }

            override fun onError() {
                super.onError()

            }
        })
    }
    private val itemChildClickListener = OnItemChildClickListener { adapter, view, position ->
        val id = view.id
        if (id == R.id.ll_mr) {
            XPopup.Builder(this)
                    .asConfirm(null, "是否设为默认") {
                        ElvesHttpUtil.getUpdateDefault(mAdapter.getItem(position).id, object : JHttpCallback() {
                            override fun onSuccess(code: Int, msg: String, info: String) {
                                if (code != 200) {
                                    ToastUtil.show(msg)
                                    return
                                }
                                setData()

                            }
                            override fun onError() {
                                super.onError()

                            }
                        })
                    }.show()
        }
        if (id == R.id.tv_delete) {
            XPopup.Builder(this)
                    .asConfirm(null, "是否确认删除") {
                        ElvesHttpUtil.getDelete(mAdapter.getItem(position).id, object : JHttpCallback() {
                            override fun onSuccess(code: Int, msg: String, info: String) {
                                if (code != 200) {
                                    ToastUtil.show(msg)
                                    return
                                }
                                setData()

                            }
                            override fun onError() {
                                super.onError()

                            }
                        })
                    }.show()
        }
    }

    override fun onDestroy() {
        Glide.get(this).clearMemory()

        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_ADDR_DEL)
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_ADDR_DEF)
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_ADDR_LIST)
        super.onDestroy()
    }
}
